﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using TimerTask.Bll;
using TimerTask.Model.Common;
using TimerTask.Model.Entity;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace TimerTask.Web.Controllers
{
    public class JobController : BaseController
    {
        // GET: /<controller>/
        public IActionResult Index()
        {
            var bll = new JobBll();
            List<GroupInfo> groupInfos = bll.GetGroupInfos();
            return View(groupInfos);
        }

        public IActionResult Get(int index = 1, int groupId = 0)
        {
            var bll = new JobBll();
            var res = bll.GetPagerJob(index, groupId);

            return PartialView("GetJobInfo", res);
        }

        public IActionResult NewSave(JobInfo model)
        {
            var bll = new JobBll();
            if (model != null)
            {
                model.CreateTime = model.UpdateTime = DateTime.Now;
            }
            MsgResult<int> res = bll.NewSave(model);
            return Json(res);
        }

        /// <summary>
        /// 开启任务
        /// author:肖阳
        /// date:2016-10-22 13:51:21
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public IActionResult Start(int jobId = 0)
        {
            var bll = new JobBll();
            MsgResult<int> result = bll.StartJob(jobId);
            return Json(result);
        }

        /// <summary>
        /// 结束任务
        /// author:肖阳
        /// date:2016-10-22 13:51:21
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public IActionResult Stop(int jobId)
        {
            var bll = new JobBll();
            MsgResult<int> result = bll.StopJob(jobId);
            return Json(result);
        }

        /// <summary>
        /// 触发任务
        /// author:肖阳
        /// date:2016-10-22 13:51:21
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        public IActionResult Trigger(int jobId)
        {
            var bll = new JobBll();
            MsgResult<int> result = bll.TriggerJob(jobId);
            return Json(result);
        }
    }
}
